package com.cyzy.controller;

import com.cyzy.service.TimetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/timetable")
@CrossOrigin
public class TimetableController {

    private final TimetableService timetableService;

    @Autowired
    public TimetableController(TimetableService timetableService) {
        this.timetableService = timetableService;
    }

    /**
     * 根据教师ID获取课程表数据
     * @param teacherId 教师ID
     * @return 课程表数据列表
     */
    @GetMapping("/{teacherId}")
    public ResponseEntity<?> getTimetableByTeacherId(@PathVariable Integer teacherId) {
        try {
            // 参数校验
            if (teacherId == null || teacherId <= 0) {
                return ResponseEntity.badRequest().body("教师ID不能为空且必须大于0");
            }

            // 调用服务层获取数据
            List<Map<String, Object>> timetableData = timetableService.getTimetableByTeacherId(teacherId);

            // 直接返回数据
            return ResponseEntity.ok(timetableData);

        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("获取课程表数据失败: " + e.getMessage());
        }
    }
}